// pages/confirm/confirm.js

const app = getApp();
import api from '../../utils/util';
const globalData = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    extendInfo: '',
    radio: '1',
    payType: "WEIXIN",
    products: [],
    total: 0,
    address: null,
    result: null,
    show: false
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  async onSubmit() {
    console.log(this.data.extendInfo)
    if (this.data.address == null) {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none'
      })
    }
    const subForm = {
      userId: app.globalData.userId,
      productId: this.data.products.reduce((pre, cur) => pre + cur.id + '', ''),
      totalPrice: this.data.total,
      status: "WAIT_BUYER_PAY",
      extendInfo: this.data.extendInfo,
      addressId: this.data.address.id
    }
    //插入订单————订单状态是待支付
    //唤醒支付————支付回调
    //1.成功————更新订单状态————插入支付记录——————跳转支付成功。
    //2.失败————跳转支付失败页面
    let result = await api.post('/order/save', subForm);
    if (result.success) {
      this.setData({
        result: result,
        show: true,
      })
    }
  },

  async onPay() {
    wx.showLoading({
      title: '支付中',
    })
    const item = this.data.products.map(e => {
      return {
        productId: e.id,
        number: e.nums
      }
    })
    const payForm = {
      userId: this.data.result.data.userId,
      orderNumber: this.data.result.data.orderNumber,
      totalAmount: this.data.total,
      payType: this.data.payType,
      pay_status: "SUCCESS",
      productItems: item
    }
    const payResult = await api.post('/payment/pay', payForm);
    setTimeout(function () {
      wx.hideLoading()
      wx.showToast({
        title: '支付成功',
        icon: "success"
      })
      wx.navigateTo({
        url: '/pages/payResult/payResult',
      })
    }, 1000)

  
  },
  onChange(event) {
    if (event.detail === '1') {
      this.setData({
        payType: "WEIXIN"
      })
    } else {
      this.setData({
        payType: "ALIPAY"
      })
    }
    this.setData({
      radio: event.detail,
    });
  },

  onClick(event) {
    const {
      name
    } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
    if (name === 1) {
      this.setData({
        payType: "WEIXIN"
      })
    } else {
      this.setData({
        payType: "ALIPAY"
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (globalData.cart.length > 0) {
      this.setData({
        products: globalData.cart,
        total: globalData.cart.reduce((pre, cur) => pre + cur.price * cur.nums, 0)
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.address.length > 0) {
      const address = app.globalData.address[app.globalData.currentRadio];
      this.setData({
        address: address
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})